package org.metasketch.demo.diagrams.PSD;

import java.util.StringTokenizer;

import org.metasketch.demo.model.CoordinationLink;
import org.metasketch.demo.model.CoordinationStep;
import org.metasketch.demo.model.CoordinationStepKind;
import org.metasketch.diagram.javafx.graphics.DefaultLink;
import org.metasketch.diagram.javafx.graphics.HorizontalLinkLabelPosition;
import org.metasketch.diagram.javafx.graphics.PositionedLabel;
import org.metasketch.diagram.javafx.graphics.VerticalLinkLabelPosition;
import org.metasketch.editor.links.LineSection;

public class CoordinationLinkNode extends DefaultLink {

	String normalStyle = "-fx-stroke: black; -fx-stroke-width: 3;  ";
	String dashedStyle = "-fx-stroke: black; -fx-stroke-width: 2;   -fx-stroke-dash-array: 12 6;    -fx-stroke-dash-offset: 6;     -fx-stroke-line-cap: butt;";

	PositionedLabel multiplicity = new PositionedLabel();

	PositionedLabel name = new PositionedLabel();

	public CoordinationLinkNode() {
		{
			// PositionedLabel label1 = new PositionedLabel();
			name.getLabel().setText("Center, Center");
			name.setHorizontal(HorizontalLinkLabelPosition.CENTER);
			name.setVertical(VerticalLinkLabelPosition.BOTTOM);
			labels.add(name);

			name.getLabel()
					.setStyle(
							"-fx-text-alignment: center; -fx-alignment: center; -fx-content-display: center;");

			name.getLabel().setHorizontallyCentered(true);
			name.getLabel().setGraphic(this);
		}

		{
			// PositionedLabel label1 = new PositionedLabel();
			multiplicity.getLabel().setText("Center,Center");
			multiplicity.setHorizontal(HorizontalLinkLabelPosition.CENTER);
			multiplicity.setVertical(VerticalLinkLabelPosition.TOP);
			labels.add(multiplicity);

			multiplicity.getLabel().setGraphic(this);
			multiplicity.getLabel().setHorizontallyCentered(true);

			multiplicity.getLabel().setStyle("-fx-text-alignment: center;");

			multiplicity.getLabel().setEditable(true);
			multiplicity.getLabel().setReadScript(
					"return semantic.lower + \"...\" + semantic.upper;");

			multiplicity
					.getLabel()
					.setWriteScript(
							"text = text.replace(\".\", \" \");	"
									+ "StringTokenizer tk = new StringTokenizer(text); "
									+ "if(tk.countTokens() < 2) return ; " +

									"String lower = tk.nextToken();" +

									"if(!lower.isNumber())	return ; " +

									"semantic.lower = lower.toInteger();" +

									"String upper = tk.nextToken();" +

									"if(!upper.isNumber()) return ;" +

									"semantic.upper = upper.toInteger();");

		}

	}

	protected void refreshModelGraphic() {
		super.refreshModelGraphic();

		String style = normalStyle;

		CoordinationLink coordinationLink = (CoordinationLink) semantic;
		if (coordinationLink.isConditional())
			style = dashedStyle;

		for (LineSection line : lines) {
			line.setStyle(style);
		}

		CoordinationLink link = (CoordinationLink) semantic;

		if (link.getSource() instanceof CoordinationStep
				&& link.getTarget() instanceof CoordinationStep) {

			CoordinationStep request = (CoordinationStep) link.getSource();
			CoordinationStep promise = (CoordinationStep) link.getTarget();

			if (request.getKind() == CoordinationStepKind.Request
					&& promise.getKind() == CoordinationStepKind.Promise) {
				name.getLabel().setVisible(true);
				name.getLabel().setText(
						request.getParentTransaction().getName());
			} else
				name.getLabel().setVisible(false);
		} else
			name.getLabel().setVisible(false);

		multiplicity.getLabel().refreshModelGraphic();
	}

}